<template>
  <div class="login-form">
    <div class="g-form">
      <div class="g-form-line">
        <div class="form-group">
          <label :style="{color:'red',background:'#fff'}">用户名:</label>
          <input type="text" name="username" class="form-control" placeholder="请输入用户名" v-model="usernameModel">
        </div>
        <span class="g-form-error">{{ userErrors.errorText }}</span>
      </div>
      <div class="g-form-line">
        <div class="form-group">
          <label>密码:</label>
          <input type="password" name="password" class="form-control" placeholder="请输入密码" v-model="passwordModel">
        </div>
        <span class="g-form-error">{{ passwordErrors.errorText }}</span>
      </div>
      <div class="g-form-line">
        <div class="form-group">
          <button type="button" class="btn btn-primary" v-on:click="onLogin">登录</button>
          <button type="reset" class="btn btn-danger">重置</button>
        </div>
      </div>
      <p>{{ errorText }}</p>
    </div>
  </div>
</template>

<script>
  export default {
    data () {
      return {
        usernameModel: '',
        passwordModel: '',
        checkPasswordModel:'',
        errorText: ''
      }
    },
    computed: {
      userErrors () {
        let errorText, status
        if (!/@/g.test(this.usernameModel)) {
          status = false
          errorText = '未包含@'
        }
        else {
          status = true
          errorText = ''
        }
        if (!this.userFlag) {
          errorText = ''
          this.userFlag = true
        }
        return {
          status,
          errorText
        }
      },
      passwordErrors () {
        let errorText, status
        if (!/^\w{1,6}$/g.test(this.passwordModel)) {
          status = false
          errorText = '密码不是1-6位'
        }
        else {
          status = true
          errorText = ''
        }
        if (!this.passwordFlag) {
          errorText = ''
          this.passwordFlag = true
        }
        return {
          status,
          errorText
        }
      }
    },
    methods: {
      onLogin () {
        alert("hello");
        if (!this.userErrors.status || !this.passwordErrors.status) {
          this.errorText = '部分选项未通过'
        }
        else {
          this.errorText = ''
          this.$http.get('http://localhost:8081/login')
            .then((res) => {
              this.$emit('has-log', res.data),
                console.log('success');
            }, (error) => {
              console.log(error)
            })
        }
      }
    }
  }
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>

</style>
